package com.zzyl.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.PostDto;
import com.zzyl.dto.RoleDto;
import com.zzyl.service.RoleService;
import com.zzyl.vo.PostVo;
import com.zzyl.vo.RoleVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RestController
@Slf4j
@Api(tags = "角色管理")
@RequestMapping("/role")
public class RoleController {
    @Autowired
    RoleService roleService;

    @PostMapping("/page/{pageNum}/{pageSize}")
    @ApiOperation(value = "角色分页",notes = "角色分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleDto",value = "角色DTO对象",required = true,dataType = "RoleDto"),
            @ApiImplicitParam(paramType = "path",name = "pageNum",value = "页码",example = "1",dataType = "Integer"),
            @ApiImplicitParam(paramType = "path",name = "pageSize",value = "每页条数",example = "10",dataType = "Integer")
    })
    public ResponseResult<PageResponse<RoleVo>> findPostVoPageResponse(
            @RequestBody RoleDto roleDto,
            @PathVariable("pageNum") int pageNum,
            @PathVariable("pageSize") int pageSize) {
        PageResponse<RoleVo> pageResponse = roleService.findPage(roleDto, pageNum, pageSize);
        return ResponseResult.success(pageResponse);
    }


    @PutMapping
    @ApiOperation(value = "角色添加",notes = "角色添加")
    public ResponseResult<RoleVo> insertRole(@RequestBody RoleDto roleDto) {
         Boolean flag = roleService.insertRole(roleDto);
        return ResponseResult.success(flag);
    }

    @GetMapping("/find-checked-resources/{roleId}")
    @ApiOperation(value = "角色添加资源管理",notes = "根据角色查询选中的资源数据")
    public ResponseResult<Set<String>> checkedResources(@PathVariable Long roleId) {
        Set<String> list = roleService.checkedResources(roleId);
        return ResponseResult.success(list);
    }

    @DeleteMapping("/{roleIds}")
    @ApiOperation(value = "删除角色表和角色对应的权限",notes = "删除角色表和角色对应的权限")
    public ResponseResult<List<String>> deleteRoleById(@PathVariable List<String> roleIds) {/*用集合接代表可长可短*/
       roleService.deleteRoleById(roleIds);
        return ResponseResult.success();
    }


    @PatchMapping
    @ApiOperation(value = "修改角色",notes = "修改角色")
    public ResponseResult<Boolean> updateRoleById(@RequestBody RoleDto roleDto) {/*用集合接代表可长可短*/
        Boolean flag = roleService.updateRoleById(roleDto);
        return ResponseResult.success(flag);
    }

    @PostMapping("/init-roles")
    @ApiOperation(value = "角色下拉框",notes = "角色下拉框")
    public ResponseResult<RoleVo> selectRoleAll() {/*查询所有*/
        List<RoleVo> role= roleService.selectRoleAll();
        return ResponseResult.success(role);
    }
}
